package com.xiangyang.pos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.xiangyang.pos.R;
import com.xiangyang.pos.adapter.OrderListRecyclerAdapter;
import com.xiangyang.pos.bean.OrderListDataBean;
import com.xiangyang.pos.constant.AppConstant;
import com.xiangyang.pos.http.HttpClient;
import com.xiangyang.pos.http.RequestParameters;
import com.xiangyang.pos.logger.Logger;
import com.xiangyang.pos.utils.SharedUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/1 0001.
 *
 * 订单列表
 *
 * @author YeDalin
 *
 */

public class OrderListActivity extends Activity implements View.OnClickListener{

    private ImageView ivGoBack;
    private RecyclerView recycler;

    private OrderListRecyclerAdapter adapter;
    private List<OrderListDataBean.OrderListData> list=new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        initView();
        setRecycler();
        initClick();
        getOrderListHttp();
        mobilePay();
    }

    private void initView() {
        ivGoBack=findViewById(R.id.iv_go_back);
        recycler=findViewById(R.id.recycler);
    }

//    初始化列表（RecyclerView）
    private void setRecycler(){
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(manager);
        adapter=new OrderListRecyclerAdapter(list,this);
        recycler.setAdapter(adapter);
    }

    private void initClick(){
        ivGoBack.setOnClickListener(this);
    }

    //转为get提交，暂时没用
    private RequestParameters getShopId(){
        RequestParameters parameters=new RequestParameters();
        parameters.put("shopId","17");
        return parameters;
    }
    //获取订单列表
    private void getOrderListHttp(){

        String shopId = SharedUtils.getStringPrefs(this, "shopId", "");

        String UserTOKEN = SharedUtils.getStringPrefs(this, "UserTOKEN", "");
        final String ORDER_LIST_URL= AppConstant.POS_URL+"/Order/An/getOrderList"+"/shopid/"+shopId+"/u="+UserTOKEN;
        HttpClient httpClient=new HttpClient();
        httpClient.post(ORDER_LIST_URL, null, new HttpClient.OnResponseListener() {
            @Override
            public void onResponse(String result) {
                Logger.e("xxxx","OrderLiset= "+result);
                if(!result.equals("Error")){
                    Gson gson=new Gson();
                    OrderListDataBean bean=gson.fromJson(result,OrderListDataBean.class);
                    if(bean!=null){
                        if(bean.getStatus()==1){
                            list.addAll(bean.getData());
                            adapter.notifyDataSetChanged();
                        }
                    }
                }else {
                    Toast.makeText(OrderListActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            Intent intent=new Intent(OrderListActivity.this,FirstActivity.class);
            startActivity(intent);
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.iv_go_back){
            Intent intent=new Intent(OrderListActivity.this,FirstActivity.class);
            startActivity(intent);
        }
    }



    //订单列表监听，scan为选择移动支付，activityGoTo为现金支付
    private void mobilePay(){
        if(adapter!=null){
            adapter.setListener(new OrderListRecyclerAdapter.OrderListListener() {
                @Override
                public void scan() {
                    customScan();
                }

                @Override
                public void activityGoTo() {
                    Intent intent=new Intent(OrderListActivity.this,OrderReceivablesActivity.class);
                    startActivityForResult(intent,1000);
                }
            });
        }
    }

    //    开始扫描
    public void customScan() {
        new IntentIntegrator(this)
                .setOrientationLocked(false)
                .setCaptureActivity(ScanActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() == null) {

            } else {
                // ScanResult 为 获取到的字符串
                String scanResult = intentResult.getContents();
                Logger.e("xxxx","payCode=  "+scanResult);

                SharedUtils.setStringPrefs(this,"payCode",scanResult);

                Intent intent=new Intent(OrderListActivity.this,OrderReceivablesActivity.class);
                startActivityForResult(intent,1000);

            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
